<?php

namespace app\admin\controller;

use app\admin\model\Product;
use app\BaseController;
use app\dic\DicProduct;
use app\dic\DicPublic;
use app\dic\DicSite;
use think\App;
use think\facade\Db;
use think\cache\driver\Redis;

/**
 * @author xjw
 * @class:TaskProduct 定时任务=>产品操作
 * @method:product 拉取商品  45分钟执行
 * @method:deal_product 处理商品相关
 * @return  
 */


class Taskcustomerjourney extends BaseController
{

    protected $model = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new Product();
    }
    
    /**
    * 4点转点之后
    * @param array params
    */
    public function automatic_list_women()
    {
        $start_at = date('Y-m-d 00:00:00', time()-16*3600-24*3600);
        $end_at = date('Y-m-d 23:59:59', time()-16*3600-24*3600);
        $where[] = ['created_at', '>=', strtotime($start_at)];
        $where[] = ['created_at', '<=', strtotime($end_at)];

        $order = table('order')    
            ->field('order_number')
            ->where('site_id','86')
            ->where($where)
            ->select()->toarray();

        table('customerjourneywomen')->insertAll($order);

        return success("完成");
    }
    /**
    * 4点转点之后
    * @param array params
    */
    public function automatic_list_woob()
    {
        $start_at = date('Y-m-d 00:00:00', time()-16*3600-24*3600);
        $end_at = date('Y-m-d 23:59:59', time()-16*3600-24*3600);
        $where[] = ['created_at', '>=', strtotime($start_at)];
        $where[] = ['created_at', '<=', strtotime($end_at)];

        $order = table('order')    
            ->field('order_number')
            ->where('site_id','29')
            ->where($where)
            ->select()->toarray();

        table('customerjourneywoob')->insertAll($order);

        return success("完成");
    }

    /**
    * 4点转点之后
    * @param array params
    */
    public function automatic_list()
    {
        $redis = new Redis();
        $start_at = date('Y-m-d 00:00:00', time()-16*3600-24*3600);
        $end_at = date('Y-m-d 23:59:59', time()-16*3600-24*3600);
        // $start_at = "2024-01-01 00:00:00";
        // $end_at = "2024-02-29 23:59:59";
        // halt($start_at);
        $page = $redis->get('customerjourney_automatic'.$start_at);
        // halt($page);
        if($page == "-1"){
            return success("完成",$page);
        }else{
            if(empty($page)){
                $page = 1;
            }
            $time = array();
            $time['start_at']= $start_at;
            $time['end_at']= $end_at;
            $msg = $this->list($page,$time);
            return success("完成",$msg);
        }
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function list($page,$time)
    {
        $redis = new Redis();
        $data = array();
        $data['direction'] = "desc";
        $data['page'] = $page;
        $data['page_size'] = 20;
        $data['shop_ids'] = 58760331325;
        $data['timezone'] = "America/Los_Angeles";
        $data['sort'] = "created_at";
        $data['start_at'] = $time['start_at'];
        $data['end_at'] = $time['end_at'];
        $url = "https://shpf.api.attribuly.com/v1-3/orders/order-list";
        $return0 = $this->posturl($url, $data);
        $return = json_decode($return0, true);
        if(!empty($return['data']['records']) && isset($return['data']['records'])){
            //添加
            $records = $return['data']['records'];
            // $inserts = array();
            foreach ($records as $k=>$v) {
                $insert = array();
                $insert['site_id'] = 121;
                $insert['remarks'] = $v['created_at'];
                if(!empty($v['created_at'])){
                    $insert['statistical_time'] = strtotime(date('Y-m-d', strtotime($v['created_at'])));
                }
                $insert['order_number'] = $v['order_number'];
                $insert['customer_name'] = $v['customer_name'];
                $insert['total_price_usd'] = $v['total_price_usd'];
                $insert['financial_status'] = $v['financial_status'];
                $insert['fulfillment_status_text'] = $v['fulfillment_status_text'];
                $insert['first_source'] = "";
                $insert['first_campaign'] = "";
                $insert['last_source'] = "";
                $insert['last_campaign'] = "";
                if(!empty($v['event']['first_event']) && isset($v['event']['first_event'])){
                    $first_event = $v['event']['first_event'];
                    $insert['first_source'] = $first_event['dm_source'];
                    $insert['first_campaign'] = $this->removeEmojis($first_event['dm_campaign']);
                }
                if(!empty($v['event']['last_event']) && isset($v['event']['last_event'])){
                    $last_event = $v['event']['last_event'];
                    $insert['last_source'] = $last_event['dm_source'];
                    $insert['last_campaign'] = $this->removeEmojis($last_event['dm_campaign']);
                }
                // $inserts[] = $insert;
                $customerjourney = table('customerjourney')->where('order_number',$insert['order_number'])->find();
                if(!empty($customerjourney)){
                    $return = table('customerjourney')->where('order_number',$insert['order_number'])->update($insert);
                }else{
                    $return = table('customerjourney')->insert($insert);
                }
            }
            // if($return){
                $page = $page + 1;
                $redis->set('customerjourney_automatic'.$time['start_at'], $page,5*24*3600);
                return "下一页";
            // }else{
            //     return "数据为空";
            // }
        }else{
            $redis->set('customerjourney_automatic'.$time['start_at'], -1);
            return "完成";
        }
    }
    
    function removeEmojis($string) {
        $regex = '/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F700}-\x{1F77F}]|[\x{1F780}-\x{1F7FF}]|[\x{1F800}-\x{1F8FF}]|[\x{1F900}-\x{1F9FF}]|[\x{1FA00}-\x{1FA6F}]|[\x{2600}-\x{26FF}]|[\x{2700}-\x{27BF}]|\p{So}/u';
        return preg_replace($regex, '', $string);
    }

    function posturl($url, $data = array())
    {
        $data = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $headerArray = array("Content-Type:application/json;", 'Authorization:Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMDQ5NDIiLCJleHAiOjE3MjEzNzUzODJ9.zxgH9y2HGY5Wiuy_3n5XPlph2f9N_VyecrDNB2F6h6g');
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
    * 方法详情
    * @param array params
    */
    public function customerj_handle()
    {
        $str = $this->config();
        $customerjourneys = table('customerjourney')
            // ->where('id',2)
            ->where('status',0)
            ->order('order_number asc')
            ->limit(10)
            ->select()->toarray();
    foreach ($customerjourneys as $k=>$customerjourney) {
        if(!empty($customerjourney)){
            // $order = table('order')
            //     ->where('order_number',$customerjourney['order_number'])
            //     ->where('site_id',$customerjourney['site_id'])
            //     // ->where('graphql_status','=',0)
            //     ->find();
            // if(empty($order) || $order['graphql_status'] == 0){
            //     return success("等待行为日志先执行");
            // }
            $customerjourneys = $this->customerjourney($customerjourney);
            //自动补全渠道标签
            if(empty($customerjourneys['tag_source']) && !empty($customerjourneys['tag_campaign'])){
                $customerjourneys['tag_source'] = $this->tag_campaign_tag($customerjourneys["tag_campaign"],$str);
            }
            $tag_source = $customerjourneys['tag_source'];
            $tag_campaign = $customerjourneys['tag_campaign'];
        }else{
            return success("暂无数据");
        }
        
        $msg_data = array();
        $update = array();
        if(!empty($tag_source)){
            $update["tag_source"] = strtolower($tag_source);
            $msg_data[] = strtolower($tag_source);
        }else{
            $update["tag_source"] = "";
        }
        if(!empty($tag_campaign)){
            $update["tag_campaign"] = strtolower($tag_campaign);
            $msg_data[] = strtolower($tag_campaign);
        }else{
            $update["tag_campaign"] = "";
        }
        // $insert = array();
        // $insert['number'] = $customerjourney['order_number'];
        // $insert['remarks'] = $customerjourney['remarks'];
        if(!empty($msg_data)){
            // $msg = implode(',',$msg_data);
            // $insert['tags'] = $msg;
            // $insert['type'] = 0;
            // $customerj_handle = table('customerj_handle')->where('number',$insert['number'])->find();
            // if(!empty($customerj_handle)){
                // table('customerj_handle')->where('number',$insert['number'])->update($insert);
            // }else{
                // table('customerj_handle')->insert($insert);
            // }
            $update['status'] = 1;
            if($update["tag_source"] == "other"){
                $update['status'] = 2;
            }
        }else{
            $update['status'] = 2;
        }
        $customerjourney = table('customerjourney')
            ->where('id',$customerjourney['id'])
            ->update($update);
    }
    return success("完成");
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function customerjourney($customerjourney)
    {
        $str = $this->config();
        $tag_source = "";
        $tag_campaign = "";
        //重写规则
            $summary = table('order')
                ->field('b.lastVisit_campaign,b.lastVisit_source,b.firstVisit_source,b.firstVisit_campaign,a.landing_site')
                ->alias('a')
                ->join('qu_customerjourneysummary b','a.id = b.order_id', 'left')
                ->where('a.order_number',$customerjourney['order_number'])
                ->where('a.site_id',$customerjourney['site_id'])
                ->find();

            //新规则 6月6
            if(!empty($summary['lastVisit_campaign']) && !empty($summary['landing_site'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $pos = strpos($summary['landing_site'], $tag_campaign);
                    if ($pos !== false) {
                        $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                        $customerjourneys['tag_campaign'] = $tag_campaign;
                        return $customerjourneys;
                    }else{
                        //情况2
                        // 如果不匹配 则使用lastVisit_campaign关键字
                        // $customerjourneys['tag_source'] = $this->tag_campaign_tag($customerjourney['last_campaign'],$str);
                        // $customerjourneys['tag_campaign'] = $tag_campaign;
                        // return $customerjourneys;
                    }
                }
            }
            if(!empty($summary['lastVisit_campaign'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            //情况3 只有landing_site有数据
            if(!empty($summary['landing_site'])){
                $tag_campaign = $this->tag_landing_site($summary['landing_site'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($tag_campaign,$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            if(!empty($summary['landing_site'])){
                $tag_source = $this->tag_site($summary['landing_site'],$str);
                if(!empty($tag_source)){
                    $customerjourneys['tag_source'] = $tag_source;
                    //0614补充
                    $customerjourneys['tag_campaign'] = "";
                    if(!empty($summary['lastVisit_campaign']) && empty($customerjourneys['tag_campaign'])){
                        $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                        if(!empty($tag_campaign)){
                            $customerjourneys_tag_source = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                            if($customerjourneys['tag_source'] == $customerjourneys_tag_source){
                                $customerjourneys['tag_campaign'] = $tag_campaign;
                            }
                        }
                    }
                    if(!empty($customerjourney['last_campaign']) && empty($customerjourneys['tag_campaign'])){
                        $tag_campaign = $this->tag_campaign($customerjourney['last_campaign'],$str);
                        if(!empty($tag_campaign)){
                            $customerjourneys_tag_source = $this->tag_campaign_tag($customerjourney['last_campaign'],$str);
                            if($customerjourneys['tag_source'] == $customerjourneys_tag_source){
                                $customerjourneys['tag_campaign'] = $tag_campaign;
                            }
                        }
                    }
                    return $customerjourneys;
                }
            }

            //0614 临时规则
            //bra60- 张兴
            if(!empty($summary['landing_site'])){
                $domain = stristr($summary['landing_site'],"bra60-");
                if($domain){
                    $customerjourneys['tag_source'] = "fb";
                    $customerjourneys['tag_campaign'] = "ZX";
                    return $customerjourneys;
                }
            }

            
            //新规则4-10
            // $last_source = $this->tag_source($customerjourney['last_source'],$str);
            // $last_campaign = $this->tag_campaign($customerjourney['last_campaign'],$str);
            
            // $lastVisit_source = $this->tag_source($summary['lastVisit_source'],$str);
            // $lastVisit_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);

            // $firstVisit_source = $this->tag_source($summary['firstVisit_source'],$str);
            // $firstVisit_campaign = $this->tag_campaign($summary['firstVisit_campaign'],$str);

            // $first_source = $this->tag_source($customerjourney['first_source'],$str);
            // $first_campaign = $this->tag_campaign($customerjourney['first_campaign'],$str);


            //新规则 优先匹配运营
            //新增规则
            // if(!empty($customerjourney['last_campaign'])){
            //     $tag_campaign = $this->tag_campaign($customerjourney['last_campaign'],$str);
            //     if(!empty($tag_campaign)){
            //         $customerjourneys['tag_source'] = $this->tag_campaign_tag($customerjourney['last_campaign'],$str);
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }
            // if(!empty($summary['lastVisit_campaign'])){
            //     $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
            //     if(!empty($tag_campaign)){
            //         $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }
            // if(!empty($summary['firstVisit_campaign'])){
            //     $tag_campaign = $this->tag_campaign($summary['firstVisit_campaign'],$str);
            //     if(!empty($tag_campaign)){
            //         $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['firstVisit_campaign'],$str);
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }
            // if(!empty($customerjourney['first_campaign'])){
            //     $tag_campaign = $this->tag_campaign($customerjourney['first_campaign'],$str);
            //     if(!empty($tag_campaign)){
            //         $customerjourneys['tag_source'] = $this->tag_campaign_tag($customerjourney['first_campaign'],$str);
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }

            //last_source
            // if(!empty($customerjourney['last_source'])){
            //     $tag_campaign = "";
            //     $tag_source = $last_source;
            //     $tag_campaign = $last_campaign;
            //     if(!empty($tag_source)){
            //         $customerjourneys['tag_source'] = $tag_source;
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }
            //order lastVisit_source
            // if(!empty($summary['lastVisit_source'])){
            //     $tag_campaign = "";
            //     $tag_source = $lastVisit_source;
            //     $tag_campaign = $lastVisit_campaign;
            //     if(!empty($tag_source)){
            //         $customerjourneys['tag_source'] = $tag_source;
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }
            //order firstVisit_source
            // if(!empty($summary['firstVisit_source'])){
            //     $tag_campaign = "";
            //     $tag_source = $firstVisit_source;
            //     $tag_campaign = $firstVisit_campaign;
            //     if(!empty($tag_source)){
            //         $customerjourneys['tag_source'] = $tag_source;
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }
            //first_source
            // if(!empty($customerjourney['first_source'])){
            //     $tag_campaign = "";
            //     $tag_source = $first_source;
            //     $tag_campaign = $first_campaign;
            //     if(!empty($tag_source)){
            //         $customerjourneys['tag_source'] = $tag_source;
            //         $customerjourneys['tag_campaign'] = $tag_campaign;
            //         return $customerjourneys;
            //     }
            // }

            // $tag_source = $this->tag_ggad($tag_source,$str);
            // if(!empty($tag_source)){
            //     $customerjourneys['tag_source'] = "other";
            //     $customerjourneys['tag_campaign'] = "";
            //     return $customerjourneys;
            // }
            //other 新规则
            //直接访问
            if($customerjourney['first_source'] == "direct" || $customerjourney['first_campaign'] == "www.google.com/organic"){
                $customerjourneys['tag_source'] = "直接访问";
                $customerjourneys['tag_campaign'] = "";
                return $customerjourneys;
            }
            if($customerjourney['last_campaign'] == "linktr.ee/referral" || $customerjourney['last_source'] == "linktr.ee"){
                $customerjourneys['tag_source'] = "fb";
                $customerjourneys['tag_campaign'] = "";
                return $customerjourneys;
            }
            if($customerjourney['last_campaign'] == "--/ads"){
                $customerjourneys['tag_source'] = "tt";
                $customerjourneys['tag_campaign'] = "";
                return $customerjourneys;
            }
            
            $customerjourneys['tag_source'] = "";
            $customerjourneys['tag_campaign'] = "";
        return $customerjourneys;
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function tag_landing_site($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str3'] as $k=>$v) {
                $domain = stristr($source,"=".$v);
                if($domain){
                    $msg = $str['str3'][$k];
                    break;
                }
            }
        }
        return $msg;
    }
    /**
    * 方法详情
    * @param array params
    */
    public function tag_site($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str10'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    $msg = $str['str11'][$k];
                    break;
                }
            }
        }
        return $msg;
    }
    /**
    * 方法详情
    * @param array params
    */
    public function tag_ggad($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str6'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    $msg = "other";
                    break;
                }
            }
        }
        return $msg;
    }
    /**
    * 方法详情
    * @param array params
    */
    public function tag_source($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str1'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    $msg = $str['str2'][$k];
                    break;
                }
            }
        }
        return $msg;
    }
    /**
    * 方法详情
    * @param array params
    */
    public function tag_campaign_tag($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str3'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    $msg = $str['str7'][$k];
                    break;
                }
            }
        }
        return $msg;
    }
    /**
    * 方法详情
    * @param array params
    */
    public function tag_campaign($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str3'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    if($v == "bra60"){
                        $msg = "ZX";
                    }elseif($v == "fei"){
                        $msg = "lxf";
                    }else{
                        $msg = $v;
                    }
                    break;
                }
            }
        }
        return $msg;
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function config()
    {
        $data['str1'] = [
            "Facebook",
            "Instagram",
            "TikTok",
            // "omnisend",
            "ad_google",
        ];
        $data['str2'] = [
            "fb",
            "fb",
            "tt",
            // "email",
            "ggad",
        ];
        $data['str3'] = [
            "lxf",
            "ZX",
            "RJW",
            "TXL",
            "MF",
            "khb",
            "HJ",
            "hsw",
            "bwh",
            "zby",
            "wwj",
            "xlh",
            "zb",
            "gjw",
        ];
        $data['str4'] = [
            "李晓飞",
            "张兴",
            "任洁文",
            "陶小兰",
            "马芳",
            "柯海波",
            "胡锦",
            "侯思维",
            "白文华",
            "章博语",
            "伍文娟",
            "许良海",
            "曾贝",
            "郭佳伟",
        ];
        $data['str5'] = [
            "64",
            "10",
            "16",
            "49",
            "76",
            "9",
            "13",
            "78",
            "47",
            "66",
            "8",
            "77",
            "12",
            "82",
        ];
        $data['str6'] = [
            "Direct",
            "google.com",
            "organic",
        ];
        $data['str7'] = [
            "fb",
            "fb",
            "fb",
            "fb",
            "ggad",
            "tt",
            "tt",
            "tt",
            "tt",
            "ggad",
            "fb",
            "tt",
            "tt",
            "fb",
        ];
        $data['str8'] = [
            "fb",
            "tt",
            "ggad",
            "直接访问",
            "email",
            "other",
            "空值",
            "impact",
        ];
        $data['str9'] = [
            "lxf",
            "ZX",
            "RJW",
            "TXL",
            "MF",
            "khb",
            "HJ",
            "hsw",
            "bwh",
            "zby",
            "wwj",
            "xlh",
            "zb",
            "gjw",
            "空值",
        ];
        $data['str10'] = [
            "fbclid",
            "gclid",
            "ttclid",
        ];
        $data['str11'] = [
            "fb",
            "ggad",
            "tt",
        ];
        return $data;
    }

    /**
    * 方法详情
    * @param array params
    */
    public function configwoob()
    {
        $data['str1'] = [
            "Facebook",
            "Instagram",
            "TikTok",
            "ad_google",
        ];
        $data['str2'] = [
            "fb",
            "fb",
            "tt",
            "ggad",
        ];
        $data['str3'] = [
            "wwj",
            "hq",
            "zb",
            "ly",
            "hj",
        ];
        $data['str4'] = [
            "伍文娟",
            "胡倩",
            "曾贝",
            "龙颖",
            "胡锦",
        ];
        $data['str5'] = [
            "8",
            "7",
            "12",
            "27",
            "13",
        ];
        $data['str6'] = [
            "Direct",
            "google.com",
            "organic",
        ];
        $data['str7'] = [
            "fb",
            "fb",
            "tt",
            "fb",
            "tt",
        ];
        $data['str8'] = [
            "fb",
            "tt",
            "ggad",
            "直接访问",
            "email",
            "other",
            "空值",
            "impact",
        ];
        $data['str9'] = [
            "wwj",
            "hq",
            "zb",
            "ly",
            "hj",
            "空值",
        ];
        $data['str10'] = [
            "fbclid",
            "gclid",
            "ttclid",
        ];
        $data['str11'] = [
            "fb",
            "ggad",
            "tt",
        ];
        return $data;
    }

    /**
    * 方法详情
    * @param array params
    */
    public function configwomen()
    {
        $data['str1'] = [
            "Facebook",
            "Instagram",
            "TikTok",
            "ad_google",
        ];
        $data['str2'] = [
            "fb",
            "fb",
            "tt",
            "ggad",
        ];
        $data['str3'] = [
            "sm",
            "hgq",
            "wzw",
        ];
        $data['str4'] = [
            "孙密",
            "胡刚强",
            "王紫薇",
        ];
        $data['str5'] = [
            "11",
            "6",
            "39",
        ];
        $data['str6'] = [
            "Direct",
            "google.com",
            "organic",
        ];
        $data['str7'] = [
            "fb",
            "fb",
            "fb",
        ];
        $data['str8'] = [
            "fb",
            "tt",
            "ggad",
            "直接访问",
            "email",
            "other",
            "空值",
            "impact",
        ];
        $data['str9'] = [
            "sm",
            "hgq",
            "wzw",
            "空值",
        ];
        $data['str10'] = [
            "fbclid",
            "gclid",
            "ttclid",
        ];
        $data['str11'] = [
            "fb",
            "ggad",
            "tt",
        ];
        return $data;
    }
    /**
    * 方法详情
    * @param array params
    */
    public function customerj_handlewoob()
    {
        $str = $this->configwoob();
        $customerjourneys = table('customerjourneywoob')
            ->where('status',0)
            ->order('order_number asc')
            ->limit(10)
            ->select()->toarray();
    foreach ($customerjourneys as $k=>$customerjourney) {
        if(!empty($customerjourney)){
            $customerjourneys = $this->customerjourneywoob($customerjourney);
 
            //去掉某些运营
            if($customerjourneys['tag_campaign'] == "wwj" || $customerjourneys['tag_campaign'] == "HJ" || $customerjourneys['tag_source'] == "ggad"){
                $customerjourneys['tag_source'] = "";
                $customerjourneys['tag_campaign'] = "";
            }

            //自动补全渠道标签
            if(empty($customerjourneys['tag_source']) && !empty($customerjourneys['tag_campaign'])){
                $customerjourneys['tag_source'] = $this->tag_campaign_tag($customerjourneys["tag_campaign"],$str);
            }
            $tag_source = $customerjourneys['tag_source'];
            $tag_campaign = $customerjourneys['tag_campaign'];
        }else{
            return success("暂无数据");
        }
        
        $msg_data = array();
        $update = array();
        if(!empty($tag_source)){
            $update["tag_source"] = strtolower($tag_source);
            $msg_data[] = strtolower($tag_source);
        }else{
            $update["tag_source"] = "";
        }
        if(!empty($tag_campaign)){
            $update["tag_campaign"] = strtolower($tag_campaign);
            $msg_data[] = strtolower($tag_campaign);
        }else{
            $update["tag_campaign"] = "";
        }
        if(!empty($msg_data)){
            $update['status'] = 1;
            if($update["tag_source"] == "other"){
                $update['status'] = 2;
            }
        }else{
            $update['status'] = 2;
        }
        $customerjourney = table('customerjourneywoob')
            ->where('id',$customerjourney['id'])
            ->update($update);
    }
    return success("完成");
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function customerjourneywoob($customerjourney)
    {
        $str = $this->configwoob();
        $tag_source = "";
        $tag_campaign = "";
        //重写规则
            $summary = table('order')
                ->field('b.lastVisit_campaign,b.lastVisit_source,b.firstVisit_source,b.firstVisit_campaign,a.landing_site')
                ->alias('a')
                ->join('qu_customerjourneysummary b','a.id = b.order_id', 'left')
                ->where('a.order_number',$customerjourney['order_number'])
                ->find();

            //新规则 6月6
            if(!empty($summary['lastVisit_campaign']) && !empty($summary['landing_site'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $pos = strpos($summary['landing_site'], $tag_campaign);
                    if ($pos !== false) {
                        $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                        $customerjourneys['tag_campaign'] = $tag_campaign;
                        return $customerjourneys;
                    }
                }
            }
            if(!empty($summary['lastVisit_campaign'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            //情况3 只有landing_site有数据
            if(!empty($summary['landing_site'])){
                $tag_campaign = $this->tag_landing_site($summary['landing_site'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($tag_campaign,$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            if(!empty($summary['landing_site'])){
                $tag_source = $this->tag_site($summary['landing_site'],$str);
                if(!empty($tag_source)){
                    $customerjourneys['tag_source'] = $tag_source;
                    //0614补充
                    $customerjourneys['tag_campaign'] = "";
                    if(!empty($summary['lastVisit_campaign']) && empty($customerjourneys['tag_campaign'])){
                        $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                        if(!empty($tag_campaign)){
                            $customerjourneys_tag_source = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                            if($customerjourneys['tag_source'] == $customerjourneys_tag_source){
                                $customerjourneys['tag_campaign'] = $tag_campaign;
                            }
                        }
                    }
                    return $customerjourneys;
                }
            }

            //0614 临时规则
            //bra60- 张兴
            if(!empty($summary['landing_site'])){
                $domain = stristr($summary['landing_site'],"bra60-");
                if($domain){
                    $customerjourneys['tag_source'] = "fb";
                    $customerjourneys['tag_campaign'] = "ZX";
                    return $customerjourneys;
                }
            }
            $customerjourneys['tag_source'] = "";
            $customerjourneys['tag_campaign'] = "";
        return $customerjourneys;
    }
    

    /**
    * 方法详情
    * @param array params
    */
    public function customerj_handlewomen()
    {
        $str = $this->config();
        $customerjourneys = table('customerjourneywomen')
            ->where('status',0)
            ->order('order_number asc')
            ->limit(10)
            ->select()->toarray();
    foreach ($customerjourneys as $k=>$customerjourney) {
        if(!empty($customerjourney)){
            $customerjourneys = $this->customerjourneywomen($customerjourney);
 
            //去掉某些运营
            if($customerjourneys['tag_campaign'] == "wwj" || $customerjourneys['tag_campaign'] == "HJ" || $customerjourneys['tag_source'] == "ggad"){
                $customerjourneys['tag_source'] = "";
                $customerjourneys['tag_campaign'] = "";
            }

            //自动补全渠道标签
            if(empty($customerjourneys['tag_source']) && !empty($customerjourneys['tag_campaign'])){
                $customerjourneys['tag_source'] = $this->tag_campaign_tag($customerjourneys["tag_campaign"],$str);
            }
            $tag_source = $customerjourneys['tag_source'];
            $tag_campaign = $customerjourneys['tag_campaign'];
        }else{
            return success("暂无数据");
        }
        
        $msg_data = array();
        $update = array();
        if(!empty($tag_source)){
            $update["tag_source"] = strtolower($tag_source);
            $msg_data[] = strtolower($tag_source);
        }else{
            $update["tag_source"] = "";
        }
        if(!empty($tag_campaign)){
            $update["tag_campaign"] = strtolower($tag_campaign);
            $msg_data[] = strtolower($tag_campaign);
        }else{
            $update["tag_campaign"] = "";
        }
        if(!empty($msg_data)){
            $update['status'] = 1;
            if($update["tag_source"] == "other"){
                $update['status'] = 2;
            }
        }else{
            $update['status'] = 2;
        }
        $customerjourney = table('customerjourneywomen')
            ->where('id',$customerjourney['id'])
            ->update($update);
    }
    return success("完成");
    }
    
    /**
    * 方法详情
    * @param array params
    */
    public function customerjourneywomen($customerjourney)
    {
        $str = $this->config();
        $tag_source = "";
        $tag_campaign = "";
        //重写规则
            $summary = table('order')
                ->field('b.lastVisit_campaign,b.lastVisit_source,b.firstVisit_source,b.firstVisit_campaign,a.landing_site')
                ->alias('a')
                ->join('qu_customerjourneysummary b','a.id = b.order_id', 'left')
                ->where('a.order_number',$customerjourney['order_number'])
                ->find();

            //新规则 6月6
            if(!empty($summary['lastVisit_campaign']) && !empty($summary['landing_site'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $pos = strpos($summary['landing_site'], $tag_campaign);
                    if ($pos !== false) {
                        $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                        $customerjourneys['tag_campaign'] = $tag_campaign;
                        return $customerjourneys;
                    }
                }
            }
            if(!empty($summary['lastVisit_campaign'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            //情况3 只有landing_site有数据
            if(!empty($summary['landing_site'])){
                $tag_campaign = $this->tag_landing_site($summary['landing_site'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($tag_campaign,$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            if(!empty($summary['landing_site'])){
                $tag_source = $this->tag_site($summary['landing_site'],$str);
                if(!empty($tag_source)){
                    $customerjourneys['tag_source'] = $tag_source;
                    //0614补充
                    $customerjourneys['tag_campaign'] = "";
                    if(!empty($summary['lastVisit_campaign']) && empty($customerjourneys['tag_campaign'])){
                        $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                        if(!empty($tag_campaign)){
                            $customerjourneys_tag_source = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                            if($customerjourneys['tag_source'] == $customerjourneys_tag_source){
                                $customerjourneys['tag_campaign'] = $tag_campaign;
                            }
                        }
                    }
                    return $customerjourneys;
                }
            }

            //0614 临时规则
            //bra60- 张兴
            if(!empty($summary['landing_site'])){
                $domain = stristr($summary['landing_site'],"bra60-");
                if($domain){
                    $customerjourneys['tag_source'] = "fb";
                    $customerjourneys['tag_campaign'] = "ZX";
                    return $customerjourneys;
                }
            }
            $customerjourneys['tag_source'] = "";
            $customerjourneys['tag_campaign'] = "";
        return $customerjourneys;
    }
}
